Hello and welcome to my first GitHub site! My name is Mia and I’m a college student in Saint Paul, Minnesota. I’ve been learning R for the past few months in my “Intro to Data Science” class, but I am still very much a beginner. Throughout this process, I have relied heavily on internet tutorials like the ones on r-bloggers.com, so I decided to make my own. I hope this tutorial can be helpful for another beginner. Coding is intimidating, but if I can do it, so can you!
In this tutorial, we’ll be exploring Spotify data – how to access it using the Spotify API and spotifyr wrapper package, as well as what all those variables actually mean. I’ve found that Spotify is a great site to get data from because the information is so widely available and they have really unique indices to quantify music. In this tutorial, we’ll be exploring three of these indices: key, speechiness, danceability. I’ll describe what each of them mean musically, then show different ways to dynamically represent them using ggplot2 and plotly.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(devtools)
library(spotifyr)
library(tidyr)
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
id <- ‘your client ID’
secret <- ‘your client secret’
Sys.setenv(SPOTIFY_CLIENT_ID = id)
Sys.setenv(SPOTIFY_CLIENT_SECRET = secret)
access_token <- get_spotify_access_token()
my_id <- '1214112002'
my_plists <- get_user_playlists(my_id)
##
|
| | 0%
|
|=================================================================| 100%
my_plists2 <- my_plists %>%
filter(playlist_name %in% c('Taiwan Top 50', 'France Top 50', 'Bolivia Top 50', 'U.S. Top 50'))
tracks <- get_playlist_tracks(my_plists2)
##
|
| | 0%
|
|================ | 25%
|
|================================ | 50%
|
|================================================= | 75%
|
|=================================================================| 100%
features <- get_track_audio_features(tracks)
tracks2 <- tracks%>%
left_join(features, by="track_uri")%>%
mutate(difference=speechiness-0.33)%>%
filter(!(track_name == "Culpables"))%>%
filter(!(track_name == "Kiss and Make Up"))
green <- "#1ed760"
yellow <- "#e7e247"
darkgray <- "#254441"
pink <- "#ff6f59"
blue <- "#17bebb"
#add track name
p3 <- ggplot(tracks2, aes(x=reorder(track_name, -difference), y=difference, fill=playlist_name,
text=paste("Speechiness:", speechiness)))+
geom_col()+
scale_fill_manual(values=c(green, yellow, pink, blue))+
theme_minimal()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.major = element_blank(),
legend.position="none")+
ylab("Speechiness Difference")+
facet_wrap(~ playlist_name)
ggplotly(p3, tooltip=c("text"))